<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Other;

/**
 * APP端轮播图管理
 *
 * @author Xmh
 */
class Carousel {

    private $Carousel;
    private $OrderSceneModel;

    public function __construct() {
        $this->Carousel = M('Carousel');
        $this->OrderSceneModel = M('OrderScene');
    }

    public function carousel_list($page = 1, $size = 10, $paramer) {
        $map = array();
        /**
         * 搜索条件
         * 1、编号
         * 2、类型：1、全部类型，2、活动，3、拍卖
         * 3、开始时间 结束时间
         * 4、排序
         * 5、备注
         * 6、状态：1、全部状态，2、已上线，3、已下线
         */
        if ($paramer['c_id']) {
            $map['c_id'] = array('LIKE', "%" . $paramer['c_id'] . "%");
        }
        if ($paramer['type']) {
            $map['type'] = array('eq', $paramer['type']);
        }
        if ($paramer['start_time']) {
            $map['publish_time'] = array('egt', $paramer['start_time']);
        }
        if ($paramer['end_time']) {
            $map['publish_time'] = array('elt', $paramer['start_time']);
        }
        if ($paramer['start_time'] && $paramer['end_time']) {
            $map['publish_time'] = array('BETWEEN', array($paramer['start_time'], $paramer['end_time']));
        }
        if ($paramer['sort']) {
            $map['sort'] = array('LIKE', "%" . $paramer['sort'] . "%");
        }
        if ($paramer['remark']) {
            $map['remark'] = array('LIKE', "%" . $paramer['remark'] . "%");
        }
        if ($paramer['status']) {
            $map['status'] = array('eq', $paramer['status']);
        }
        if ($paramer['platform']) {
            $map['platform'] = array('eq', $paramer['platform']);
        }
        $map['_logic'] = 'AND';

        $cols = 'c_id,focus_picture,type,param_url,scene_id,publish_time,sort,remark,status,platform';
        $order = 'status,sort,c_id DESC';
        $total = $this->Carousel->where($map)->count();
        $list = $this->Carousel->field($cols)
                ->where($map)
                ->limit($size)
                ->page($page)
                ->order($order)
                ->select();
//        echo $this->Carousel->getLastSql();
        $list_info = array();
        foreach ($list as $k => $v) {
            $list_info[$k]['c_id'] = $v['c_id'];
            $list_info[$k]['type'] = $v['type'];
            $list_info[$k]['focus_picture'] = $v['focus_picture'];
            if ($v['type'] == '1') {
                $scene = $this->OrderSceneModel->where(array('scene_id' => $v['scene_id']))->find();
                $list_info[$k]['scene_id'] = $v['scene_id'];
                $list_info[$k]['name'] = $scene['name'];
            } else {
                $list_info[$k]['param_url'] = $v['param_url'];
            }
            $list_info[$k]['publish_time'] = $v['publish_time'];
            $list_info[$k]['sort'] = $v['sort'];
            $list_info[$k]['remark'] = $v['remark'];
            $list_info[$k]['status'] = $v['status'];
            $list_info[$k]['platform'] = $v['platform'];
        }
        $result = array(
            'list' => $list_info,
            'total' => $total
        );
        return $result;
    }

    /**
     * 新建轮播图
     */
    public function add_carousel($data = array()) {
        return $this->Carousel->add($data);
    }

    /**
     * 编辑轮播图
     */
    public function save_carousel($data = array()) {
        return $this->Carousel->save($data);
    }

    public function get_carousel($cid) {
        return $this->Carousel->where(array('c_id' => $cid))->find();
    }

    /**
     * 上下线操作
     */
    public function carousel_status_post($cid, $sort, $status,$platform) {
        $condition['c_id'] = $cid;
        $data['status'] = $status;
        if ($status == '1') {     //如果上线操作
            $map['status'] = '1';
            $map['sort'] = $sort;
            $map['platform'] = $platform;
            $total = $this->Carousel->where($map)->count();
            if ($total > 0) {
                return false;
            }
        }
        return $this->Carousel->where($condition)
                        ->setField($data);
    }

    /**
     * 上线操作
     */
    public function up($cid) {
        $condition['c_id'] = $cid;
        $data['status'] = '1';
        return $this->Carousel->where($condition)
                        ->setField($data);
    }

    /**
     * 下线操作
     */
    public function down($cid) {
        $condition['c_id'] = $cid;
        $data['status'] = '2';
        return $this->Carousel->where($condition)
                        ->setField($data);
    }

    /**
     * 判断排序是否在上线状态中的焦点图是否存在
     */
    public function is_sort_online($sort,$platform) {
        $condition['status'] = '1';
        $condition['sort'] = $sort;
        $condition['platform'] = $platform;
        return $this->Carousel->where($condition)->count();
    }

}
